/********************************************************************************
 * Project Name		[JavaSE_Fundamentals]
 * File Name     	[TestRandomAccessFile.java]
 * Creation Date 	[2015-01-01]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ********************************************************************************/
package io.api;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * java.io.RandomAccessFile测试<br/>
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 * 
 */
public class TestRandomAccessFile {

	/**
	 * 使用java.io.RandomAccessFile追加拷贝文件
	 * 
	 * @param fileSource
	 * @param fileDest
	 */
	public static void copyFileByRandomAccess(String fileSource, String fileDest) {
		try {
			RandomAccessFile in = new RandomAccessFile(fileSource, "r");
			RandomAccessFile out = new RandomAccessFile(fileDest, "rw");

			long fileLength = out.length(); // 要写出的文件已有的长度
			out.seek(fileLength); // 将文件指针移到文件尾部

			byte[] buffer = new byte[(int) in.length()];
			in.read(buffer);
			out.write(buffer);

			in.close();
			out.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		GameRecorder recorder = new GameRecorder();
		recorder.record("sean", 100);
		recorder.record("sean", 80);
		recorder.record("cc", 120);
		recorder.record("cc", 200);

		recorder.listAllRecords();
	}
}

/**
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 *
 */
class GameRecorder {

	public String fileName = "test_raf.txt";

	/**
	 * 记录分数
	 * 
	 * @param breakerName
	 * @param score
	 */
	public void record(String breakerName, int score) {
		// "r" 以只读的方式打开
		// "rw" 打开以便读取和写入
		RandomAccessFile raf = null;

		try {
			raf = new RandomAccessFile(fileName, "rw");
			boolean flag = false;

			// 如果未读到文件结尾
			while (raf.getFilePointer() < raf.length()) {
				String name = raf.readUTF();
				if (breakerName.equals(name)) {
					raf.writeInt(score);
					flag = true;
					break;
				} else {
					// 每个人的记录数为int类型, 大小为4个字节, 一次呢跳过四个字节到下一个人名字之前
					raf.skipBytes(4);
				}
			}

			if (!flag) {
				raf.writeUTF(breakerName);
				raf.writeInt(score);
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (raf != null) {
					raf.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public void listAllRecords() {
		RandomAccessFile raf = null;

		try {
			raf = new RandomAccessFile(fileName, "r");
			while (raf.getFilePointer() < raf.length()) {
				String name = raf.readUTF();
				int score = raf.readInt();
				System.out.println("名字是: " + name + " 分数是: " + score);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (raf != null) {
					raf.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
